package com.zhengtoudai.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.BaseActivity;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerRealNameCertificationActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.injector.module.RealNameCertificationActivityModule;
import com.zhengtoudai.ui.presenter.ShengTenpayPresenterImpl;
import com.zhengtoudai.ui.view.ShengTenpeyView;
import com.zhengtoudai.util.Tool;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RealNameCertificationActivity extends BaseActivity implements ShengTenpeyView{

    @Bind(R.id.edt_real_name)
    EditText mEdtRealName;
    @Bind(R.id.edt_id_number)
    EditText mEdtIdNumber;


    @Inject
    ShengTenpayPresenterImpl mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_name_certification);
        ButterKnife.bind(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("绑定盛付通");
    }

    @Override
    protected void initView() {
        DaggerRealNameCertificationActivityComponent.builder()
                .appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(this))
                .realNameCertificationActivityModule(new RealNameCertificationActivityModule())
                .build()
                .inject(this);
        mPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }

    @OnClick(R.id.btn_certification)
    public void onClick() {
        if (Tool.isFastDoubleClick()) return;
        mEdtRealName.setError(null);
        mEdtIdNumber.setError(null);
        String realName=mEdtRealName.getText().toString().trim();
        String idCardNo=mEdtIdNumber.getText().toString().trim();
        if (TextUtils.isEmpty(realName)){
            mEdtRealName.setError("请输入您的真实姓名");
            mEdtRealName.requestFocus();
        }else if (TextUtils.isEmpty(idCardNo)) {
            mEdtIdNumber.setError("请输入您的身份证号码");
            mEdtIdNumber.requestFocus();
        }else if (idCardNo.length()!=15&&idCardNo.length()!=18){
            mEdtIdNumber.setError("请输入正确的身份证号码");
            mEdtIdNumber.requestFocus();
        }else{
            Map<String,String> params=new ConcurrentHashMap<>();
            params.put("realName",realName);
            params.put("idCardNo",idCardNo);
            mPresenter.postShengTenpay(Config.BASE_URL+"appAccountCorrelation/toRegister.app",params);
        }
    }

    @Override
    public void showDialog() {
        showProgressDialog("正在跳转中...");
    }

    @Override
    public void onFailure(Throwable e) {
        HttpUtil.disposeOnFailure(context,e);
    }

    @Override
    public void onResult(String html) {
        Bundle bundle = new Bundle();
        bundle.putString("title", "盛付通");
        bundle.putString("html", html);
        startActivity(bundle, WebViewActivity.class);
        finish();
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }
}
